
class Admin::ServiceNetworksController < Admin::BaseController

   def index
    @service_networks = ServiceNetwork.with_deleted.paginate(page: params[:page] || 1, per_page: params[:per_page] || 15).order("id desc")
  end

  def new
    @service_network = ServiceNetwork.new
  end

  def create
    @service_network = ServiceNetwork.new(params.require(:service_network).permit!)
    if @service_network.save
      flash[:notice] = "新建服务网点成功"
      redirect_to admin_service_networks_path
    else
      render action: :new
    end
  end

  def edit
    @service_network = ServiceNetwork.with_deleted.find params[:id]
    render action: :new
  end

  def update
    @service_network = ServiceNetwork.with_deleted.find params[:id]
    @service_network.attributes = params.require(:service_network).permit!

    if @service_network.save
      flash[:notice] = "修改服务网点成功"
      redirect_to admin_service_networks_path
    else
      render action: :new
    end
  end

  def destroy
    @service_network = ServiceNetwork.with_deleted.find params[:id]

    begin
      @service_network.destroy
      flash[:notice] = "删除服务网点成功"
      redirect_to admin_service_networks_path
    rescue
      flash[:notice] = "删除服务网点失败"
      redirect_to :back
    end
  end

end
